<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <base href="${pageContext.request.contextPath}/">
    <link href="bootstrap/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
    <%--bootstrap-table核心css--%>
    <link rel="stylesheet" type="text/css" href="bootstrap-table/bootstrap-table.min.css">
    <%--bootstrap-datetimepicker核心css--%>
    <link rel="stylesheet" type="text/css" href="bootstrap-datetimepicker/css/bootstrap-datetimepicker.min.css">

</head>
<body>

<!-- 创建客户的模态窗口 -->
<div class="modal fade" id="createCustomerModal" role="dialog">
    <div class="modal-dialog" role="document" style="width: 85%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">×</span>
                </button>
                <h4 class="modal-title" id="myModalLabel1">创建客户</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal dataForm" role="form">

                    <div class="form-group">
                        <label for="create-customerOwner" class="col-sm-2 control-label">所有者<span style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">
                            <select class="form-control ownerSelect" id="create-customerOwner"></select>
                            <span class="errorMsg" style="color: red"></span>
                        </div>
                        <label for="create-customerName" class="col-sm-2 control-label">名称<span style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="create-customerName">
                            <span class="errorMsgName" style="color: red"></span>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="create-website" class="col-sm-2 control-label">公司网站</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="create-website">
                        </div>
                        <label for="create-phone" class="col-sm-2 control-label">公司座机</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="create-phone">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="create-description" class="col-sm-2 control-label">描述</label>
                        <div class="col-sm-10" style="width: 81%;">
                            <textarea class="form-control" rows="3" id="create-description"></textarea>
                        </div>
                    </div>
                    <div style="height: 1px; width: 103%; background-color: #D5D5D5; left: -13px; position: relative;"></div>

                    <div style="position: relative;top: 15px;">
                        <div class="form-group">
                            <label for="create-contactSummary" class="col-sm-2 control-label">联系纪要</label>
                            <div class="col-sm-10" style="width: 81%;">
                                <textarea class="form-control" rows="3" id="create-contactSummary"></textarea>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">下次联系时间</label>
                            <div class="col-sm-10" style="width: 300px;">
                                <div class="input-group date myDatetime">
                                    <input type="text" class="form-control" id="create-nextContactTime" readonly/>
                                    <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div style="height: 1px; width: 103%; background-color: #D5D5D5; left: -13px; position: relative; top : 10px;"></div>

                    <div style="position: relative;top: 20px;">
                        <div class="form-group">
                            <label for="create-address1" class="col-sm-2 control-label">详细地址</label>
                            <div class="col-sm-10" style="width: 81%;">
                                <textarea class="form-control" rows="1" id="create-address1"></textarea>
                            </div>
                        </div>
                    </div>
                </form>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="saveCustomer()">保存</button>
            </div>
        </div>
    </div>
</div>

<!-- 修改客户的模态窗口 -->
<div class="modal fade" id="editCustomerModal" role="dialog">
    <div class="modal-dialog" role="document" style="width: 85%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">×</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">修改客户</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal dataForm" role="form">
                    <input type="hidden" id="edit-id">
                    <div class="form-group">
                        <label for="edit-customerOwner" class="col-sm-2 control-label">所有者<span style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">
                            <select class="form-control ownerSelect" id="edit-customerOwner"></select>
                            <span class="edit-errorMsg" style="color: red"></span>
                        </div>
                        <label for="edit-customerName" class="col-sm-2 control-label">名称<span style="font-size: 15px; color: red;">*</span></label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="edit-customerName">
                            <span class="edit-errorMsgName" style="color: red"></span>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="edit-website" class="col-sm-2 control-label">公司网站</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="edit-website">
                        </div>
                        <label for="edit-phone" class="col-sm-2 control-label">公司座机</label>
                        <div class="col-sm-10" style="width: 300px;">
                            <input type="text" class="form-control" id="edit-phone">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="edit-description" class="col-sm-2 control-label">描述</label>
                        <div class="col-sm-10" style="width: 81%;">
                            <textarea class="form-control" rows="3" id="edit-description"></textarea>
                        </div>
                    </div>

                    <div style="height: 1px; width: 103%; background-color: #D5D5D5; left: -13px; position: relative;"></div>

                    <div style="position: relative;top: 15px;">
                        <div class="form-group">
                            <label for="edit-contactSummary" class="col-sm-2 control-label">联系纪要</label>
                            <div class="col-sm-10" style="width: 81%;">
                                <textarea class="form-control" rows="3" id="edit-contactSummary"></textarea>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">下次联系时间</label>
                            <div class="col-sm-10" style="width: 300px;">
                                <div class="input-group date myDatetime">
                                    <input type="text" class="form-control" id="edit-nextContactTime" readonly/>
                                    <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div style="height: 1px; width: 103%; background-color: #D5D5D5; left: -13px; position: relative; top : 10px;"></div>

                    <div style="position: relative;top: 20px;">
                        <div class="form-group">
                            <label for="edit-address" class="col-sm-2 control-label">详细地址</label>
                            <div class="col-sm-10" style="width: 81%;">
                                <textarea class="form-control" rows="1" id="edit-address"></textarea>
                            </div>
                        </div>
                    </div>
                </form>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="editCustomer()">更新</button>
            </div>
        </div>
    </div>
</div>




<div>
    <div style="position: relative; left: 10px; top: -10px;">
        <div class="page-header">
            <h3>客户列表</h3>
        </div>
    </div>
</div>

<div style="position: relative; top: -20px; left: 0px; width: 100%; height: 100%;">

    <div style="width: 100%; position: absolute;top: 5px; left: 10px;">

        <div class="btn-toolbar" role="toolbar" style="height: 80px;">
            <form id="searchForm" class="form-inline" role="form" style="position: relative;top: 8%; left: 5px;">

                <div class="form-group">
                    <div class="input-group">
                        <div class="input-group-addon">名称</div>
                        <input id="Customer-name" class="form-control" type="text">
                    </div>
                </div>

                <div class="form-group">
                    <div class="input-group">
                        <div class="input-group-addon">所有者</div>
                        <input id="Customer-owner" class="form-control" type="text">
                    </div>
                </div>

                <div class="form-group">
                    <div class="input-group">
                        <div class="input-group-addon">公司座机</div>
                        <input id="Customer-phone" class="form-control" type="text">
                    </div>
                </div>

                <div class="form-group">
                    <div class="input-group">
                        <div class="input-group-addon">公司网站</div>
                        <input id="Customer-website" class="form-control" type="text">
                    </div>
                </div>
                <div class="form-group">
                    <button type="button" class="btn btn-primary" onclick="$('#tab').bootstrapTable('refresh');">
                        <span class="glyphicon glyphicon-search"> 搜索</span>
                    </button>
                    <button type="button" class="btn btn-primary" onclick="$('#searchForm')[0].reset();$('#tab').bootstrapTable('refresh');">
                        <span class="glyphicon glyphicon-refresh"> 清空</span>
                    </button>
                </div>


            </form>
        </div>
        <div class="btn-toolbar" role="toolbar" style="background-color: #F7F7F7; height: 50px; position: relative;top: 5px;">
            <div class="btn-group" style="position: relative; top: 18%;">
                <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#createCustomerModal"><span class="glyphicon glyphicon-plus"></span> 创建</button>
                <button type="button" class="btn btn-danger" onclick="removeBatch()"><span class="glyphicon glyphicon-minus"></span> 批量删除</button>
            </div>

        </div>
        <div style="position: relative;top: 10px;">
            <table id="tab" class="table table-hover table-striped table-bordered" style="width: 98%;"></table>
        </div>
    </div>
</div>

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
<%--bootstrap-table核心js--%>
<script type="text/javascript" src="bootstrap-table/bootstrap-table.min.js"></script>
<%--bootstrap-table中文语言包--%>
<script type="text/javascript" src="bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
<%--bootstrap-datetimepicker核心js--%>
<script type="text/javascript" src="bootstrap-datetimepicker/js/bootstrap-datetimepicker.min.js"></script>
<%--bootstrap-datetimepicker中文语言包--%>
<script type="text/javascript" src="bootstrap-datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js"></script>

<script type="text/javascript">

    $(function(){

        //定制字段
        $("#definedColumns > li").click(function(e) {
            //防止下拉菜单消失
            e.stopPropagation();
        });

        //时间加载函数
        $('.myDatetime').datetimepicker({
            language: 'zh-CN',
            format: 'yyyy-mm-dd', //日期格式
            weekStart: 1, //一周从周一开始
            autoclose: true, //选择日期后关闭
            startView: 'month', //默认打开月
            minView: 'month',   //精确到年月日，不带时分秒
            todayBtn: true,
            forceParse: false
        });

        //清空模态框中的表单
        $('#createCustomerModal').on('hidden.bs.modal', function () {
            $('.errorMsg').html('');
            $('.dataForm')[0].reset();
        });

        $('#editCustomerModal').on('hidden.bs.modal', function () {
            $('.errorMsg').html('');
            $('.dataForm')[1].reset();
        });

        //初始化客户表格
        initTable();

        //初始化下拉列表
        initRelation();
    });

    //初始化表格
    function initTable() {
        //调用组件配置异步表格
        $('#tab').bootstrapTable({
            //http请求方式
            method: 'get',
            url: 'customer/page',
            //工具栏
            toolbar: '#toolbar',
            //点击选中
            clickToSelect: true,
            //单选
            singleSelect: false,
            //处理响应数据格式
            responseHandler: function (res) {
                if (res.code == 0) {
                    return {
                        rows: res.data.list,
                        total: res.data.total
                    };
                }
            },
            //开启分页
            pagination: true,
            //设置服务器端分页
            sidePagination: 'server',
            //设置每页条数
            pageSize: 10,
            //设置每页条数列表
            pageList: [10, 20, 50, 100],
            paginationPreText: '上一页',
            paginationNextText: '下一页',
            //设置bootstrapTable向后台提交分页数据
            queryParamsType: '',
            //查询参数
            queryParams: function (params) {
                params.owner = $('#Customer-owner').val();
                params.name = $('#Customer-name').val();
                params.phone = $('#Customer-phone').val();
                params.website = $('#Customer-website').val();
                return params;
            },
            showRefresh: true,
            showToggle: true,
            showFullscreen: true,
            //表格中列描述
            columns: [
                {
                    checkbox: true
                }, {
                    field: 'id',
                    visible: false
                }, {
                    title: '名称',
                    field: 'name',
                    halign: 'center',
                    align: 'center',
                    //列数据格式化
                    formatter: function (value, row) {
                        //格式化之后的列数据
                        return '<a href="customer/detail/' + row.id + '">' + value + '</a>';
                    }
                }, {
                    title: '所有者',
                    field: 'userName',
                    halign: 'center',
                    align: 'center',
                }, {
                    title: '公司座机',
                    field: 'phone',
                    halign: 'center',
                    align: 'center',
                }, {
                    title: '公司网站',
                    field: 'website',
                    halign: 'center',
                    align: 'center',
                }, {
                    title: '操作',
                    halign: 'center',
                    align: 'center',
                    formatter: function (value, row) {
                        return '<button type="button" title="编辑" class="btn btn-warning btn-xs" onclick="get(\'' + row.id +
                            '\')"><span class="glyphicon glyphicon-edit"></span></button>&emsp;' +
                            '<button type="button" title="删除" class="btn btn-danger btn-xs" onclick="remove(\'' + row.id +
                            '\')"><span class="glyphicon glyphicon-trash"></span></button>';
                    }
                }
            ]
        });
    }

    //加载关联信息，初始化下拉列表
    function initRelation() {
        $.ajax('user/list', {
            type: 'get',
            success: function (res) {
                if(res.code == 0){
                    var array = res.data;
                    var str = '<option value=""></option>';
                    $.each(array, function () {
                        str += '<option value="' + this.id + '">' + this.name + '</option>';
                    });
                    $('.ownerSelect').html(str);
                }
            }
        })
    }

    //新增
    function saveCustomer() {
        $.ajax('customer/add', {
            type: 'post',
            contentType: 'application/json',
            data: JSON.stringify({
                owner: $('#create-customerOwner').val(),
                name: $('#create-customerName').val(),
                phone: $('#create-phone').val(),
                website: $('#create-website').val(),
                description: $('#create-description').val(),
                contactSummary: $('#create-contactSummary').val(),
                nextContactTime: $('#create-nextContactTime').val(),
                address: $('#create-address').val()
            }),
            beforeSend: function () {
                //前端数据校验
                var owner = $('#create-customerOwner').val();
                if (!owner) {
                    $('.errorMsg').html('请选择所有者');
                    return false;
                }else {
                    $('.errorMsg').html('');
                }
                var name = $('#create-customerName').val();
                if (!name) {
                    $('.errorMsgName').html('客户名称不能为空');
                    return false;
                }else {
                    $('.errorMsgName').html('');
                }
            },
            success: function (res) {
                if (res.code == 0) {
                    alert('新增成功');
                    //刷新表格
                    $('#tab').bootstrapTable('refresh');
                    //关闭窗体
                    $('#createCustomerModal').modal('hide');
                }else{
                    $('.errorMsg').html(res.message);
                }
            }
        });
    }

    //打开修改
    function get(id) {
        $.ajax('customer/find/' + id, {
            type: 'get',
            success: function (res) {
                if (res.code == 0) {
                    $('#edit-id').val(res.data.id);
                    $('#edit-customerOwner').val(res.data.owner);
                    $('#edit-customerName').val(res.data.name);
                    $('#edit-phone').val(res.data.phone),
                    $('#edit-website').val(res.data.website),
                    $('#edit-description').val(res.data.description),
                    $('#edit-contactSummary').val(res.data.contactSummary),
                    $('#edit-nextContactTime').val(res.data.nextContactTime),
                    $('#edit-address').val(res.data.address)
                    $('#editCustomerModal').modal('show');
                }else if(res.code = -100){
                    // location.href = 'page/login';
                    parent.location.href = 'page/login';
                }else{
                    alert(res.message);
                }
            }
        });
    }

    //修改
    function editCustomer() {
        $.ajax('customer/edit', {
            type: 'put',
            contentType: 'application/json',
            data: JSON.stringify({
                id: $('#edit-id').val(),
                owner: $('#edit-customerOwner').val(),
                name: $('#edit-customerName').val(),
                phone: $('#edit-phone').val(),
                website: $('#edit-website').val(),
                description: $('#edit-description').val(),
                contactSummary: $('#edit-contactSummary').val(),
                nextContactTime: $('#edit-nextContactTime').val(),
                address: $('#edit-address').val()
            }),
            beforeSend: function () {
                //前端数据校验
                var owner = $('#edit-customerOwner').val();
                if (!owner) {
                    $('.edit-errorMsg').html('请选择所有者');
                    return false;
                }else {
                    $('.edit-errorMsg').html('');
                }
                var name = $('#edit-customerName').val();
                if (!name) {
                    $('.edit-errorMsgName').html('客户名称不能为空');
                    return false;
                }else {
                    $('.errorMsgName').html('');
                }
            },
            success: function (res) {
                if (res.code == 0) {
                    alert('编辑成功');
                    //刷新表格
                    $('#tab').bootstrapTable('refresh');
                    //关闭窗体
                    $('#editCustomerModal').modal('hide');
                }else{
                    $('.edit-errorMsg').html(res.message);
                }
            }
        });
    }

    //删除
    function remove(id) {
        if (confirm('确定删除数据吗？')) {
            $.ajax('customer/deleteById/' + id, {
                type: 'delete',
                success: function (res) {
                    if (res.code == 0) {
                        alert('删除成功');
                        $('#tab').bootstrapTable('refresh');
                    }else{
                        alert(res.message);
                    }
                }
            });
        }
    }

    //批量删除
    function removeBatch() {
        //获取用户在表格中选择
        var row = $('#tab').bootstrapTable('getSelections');
        if (row.length == 0) {
            alert('请选择需要删除的数据.');
        } else {
            if (!confirm('确定删除数据吗?')) {
                return;
            }
            var ids = [];
            $.each(row, function () {
                ids.push(this.id);
            });
            $.ajax('customer/removes', {
                type: 'post',
                data: {
                    _method: 'delete',
                    ids: ids.join()
                },
                success: function (res) {
                    if (res.code == 0) {
                        alert('删除成功');
                        $('#tab').bootstrapTable('refresh');
                    }else{
                        alert(res.message);
                    }
                }
            })
        }
    }

</script>
</body>
</html>